package cn.itsource.pet.controller;

import cn.itsource.basic.utils.LoginContext;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Logininfo;
import cn.itsource.org.domain.User;
import cn.itsource.org.domain.dto.AcceptDto;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.UserMapper;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.query.SearchMasterMsgQuery;
import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;




    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务器忙，请稍后重试");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务器忙，请稍后重试");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryPage(query);
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg msg, HttpServletRequest request){
        try {
            String username = LoginContext.getCurrentUser(request).getUsername();
            User user = userMapper.loadByUserName(username);
            msg.setUserId(user.getId());
            searchMasterMsgService.publish(msg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 个人中心我发布的
     * @param query
     * @return
     */
    @PostMapping("/posted")
    public PageList<SearchMasterMsg> posted(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request)
    {
        Logininfo currentUser = LoginContext.getCurrentUser(request);
        User user = userMapper.loadByUserName(currentUser.getUsername());
        query.setUserId(user.getId());
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 后端审核未通过
     * @param query
     * @return
     */
    @PostMapping("/notPass")
    public PageList<SearchMasterMsg> notPass(@RequestBody SearchMasterMsgQuery query)
    {
        query.setState(-1);
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 寻主池消息
     * @param query
     * @return
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query)
    {
        query.setState(1);
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 店铺待处理
     * @param query
     * @return
     */
    @PostMapping("/shopHandler")
    public PageList<SearchMasterMsg> shopHandler(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request)
    {
        Logininfo currentUser = LoginContext.getCurrentUser(request);
        Employee employee = employeeMapper.queryByLoginInfoId(currentUser.getId());
        query.setState(1);
        query.setShopId(employee.getShopId());
        return searchMasterMsgService.queryPage(query);
    }

    /**
     * 已完成
     * @param query
     * @return
     */
    @PostMapping("/complete")
    public PageList<SearchMasterMsg> complete(@RequestBody SearchMasterMsgQuery query)
    {
        query.setState(2);
        return searchMasterMsgService.queryPage(query);
    }

    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto dto){
        try {
            searchMasterMsgService.accept(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

}
